﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Entities.Content;
using DotNetNuke.Entities.Content.Common;
using DotNetNuke.Common.Utilities;

namespace RickDnn.Modules.DNNTaskManager.Components.Taxonomy
{
    public class Content
    {
        private const string ContentTypeName = "DNNTaskManagerTask";
        /// <summary>
        /// This should only run after the Task exists in the data store
        /// </summary>
        /// 
        /// <returns>The newly created ContentItemID in the store</returns>
        public ContentItem CreateContentItem(Task objTask,int tabId)
        {
            var typeController = new ContentTypeController();
            var colContentTypes = (from t in typeController.GetContentTypes() where t.ContentType==ContentTypeName select  t);
            int contentTypeId;

            if (colContentTypes.Count()>0)
            {
                var contentType = colContentTypes.Single();
                contentTypeId = contentType == null ? CreateContentType():contentType.ContentTypeId;
            }
            else
            {
                contentTypeId = CreateContentType();
            }

            var objContent = new ContentItem
            {
                Content = objTask.TaskName + " " + objTask.TaskDescription,
                ContentTypeId = contentTypeId,
                Indexed = false,
                ContentKey = "tid=" + objTask.TaskId,
                ModuleID = objTask.ModuleId,
                TabID = tabId
            };
            objContent.ContentItemId = Util.GetContentController().AddContentItem(objContent);
            //Add terms
            var cntTerm = new Terms();
            cntTerm.ManageTaskTerms(objTask,objContent);
            return objContent;
        }
        /// <summary>
        /// This is used to update the content in the ContentItem table.
        /// Should be called when a Task is updated
        /// </summary>      
        public void UpdateContentItem(Task objTask,int tabId)
        {
            var objContent = Util.GetContentController().GetContentItem(objTask.ContentItemId);

            if (objContent == null) return;
            objContent.Content = objTask.TaskName + " " + objTask.TaskDescription;
            objContent.TabID = tabId;
            Util.GetContentController().UpdateContentItem(objContent);

            //Update Terms
            var cntTerm = new Terms();
            cntTerm.ManageTaskTerms(objTask, objContent);

        }


        /// <summary>
        /// This removes a content item associated with a task from the data store.Should run every time
        /// a task is removed
        /// </summary>
        /// <param name="objTask">The Content Item we wish to remove from the data store </param>
        public void DeleteContentItem(Task objTask)
        {
            if (objTask.ContentItemId<=Null.NullInteger) return;
            var objContent = Util.GetContentController().GetContentItem(objTask.ContentItemId);

            if (objContent == null) return;

            //remove any matched metadata/terms associated first (perhaps we should just rely on ContentItem css)          
            var cntTerms = new Terms();
            cntTerms.RemoveTaskTerms(objTask);
            Util.GetContentController().DeleteContentItem(objContent);

        }

        #region Provide Methods
        /// <summary>
        /// Create a content Type (from taxonomy) in the data store.
        /// </summary>
        /// <returns>The primary Key value of the new ContentType </returns>
        private static int CreateContentType()
        {
            var typeController = new ContentTypeController();
            var objContentType = new ContentType { ContentType = ContentTypeName };

            return typeController.AddContentType(objContentType);
        }
        #endregion
    }
}